/*
* Copyright 2004 Christopher M. Bouzek 
*/

using System;
using SMS.SMPPClient.PDU;

namespace SMS.SMPPClient.PDU.Incoming
{
	/// <summary>
	/// This class defines the response to a query_sm PDU.
	/// </summary>
	public class SMPPQuerySMResponse : SMPPResponse
	{
		private string _MessageID;
		private string _FinalState;
		private byte _MessageStatus;
		private byte _ErrorCode;

		/// <summary>
		/// Enumerates the different states a message can be in.
		/// </summary>
		public enum MessageStateType : byte
		{
			Enroute = 1,
			Delivered = 2,
			Expired = 3,
			Deleted = 4,
			Undeliverable = 5,
			Accepted = 6,
			Unknown = 7,
			Rejected = 8
		}

		/// <summary>
		/// SMSC Message ID of the message whose state is being queried.
		/// </summary>
		public string MessageID
		{

			get
			{
				return _MessageID;
			}
		}

		/// <summary>
		/// Date and time when the queried message reached a final state. For messages
		/// which have not yet reached a final state this field will be null.
		/// </summary>
		public string FinalState
		{

			get
			{
				return _FinalState;
			}
		}

		/// <summary>
		/// Specifies the status of the queried short message.
		/// </summary>
		public MessageStateType MessageStatus
		{
			get
			{
				return ( MessageStateType )Enum.Parse( typeof( MessageStateType ),
														Enum.GetName( typeof( MessageStateType ),
																		_MessageStatus ) );
			}
		}

		/// <summary>
		/// Holds a network error code defining the reason for failure of message delivery.
		/// </summary>
		public byte ErrorCode
		{
			get
			{
				return _ErrorCode;
			}
		}

		/// <summary>
		/// Creates a query_sm response.
		/// </summary>
		public SMPPQuerySMResponse( byte[] response ) : base( response )
		{
			DecodeSMSCResponse( );
		}

		/// <summary>
		/// Decodes the query_sm response from the SMSC.
		/// </summary>
		protected override void DecodeSMSCResponse( )
		{
			byte[] remainder = ResponseAfterHeader;
			_MessageID = GetStringFromBody( ref remainder );
			_FinalState = GetStringFromBody( ref remainder );
			_MessageStatus = remainder[0];
			_ErrorCode = remainder[1];

			//fill the TLV table if applicable
			tlvTable.TranslateTLVDataIntoTable( remainder, 2 );
		}
	}
}
